package com.shujia.gma.controller;

import com.shujia.gma.bean.ProvinceIndex;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;

@RestController
public class ProvinceController {

    private static Connection con;

    static {
        //1、加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //建立连接
        try {
            con = DriverManager.getConnection("jdbc:mysql://node1:3306/gma_ads?useUnicode=true&characterEncoding=UTF-8", "root", "123456");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getProvinceIndex")
    public  ArrayList<ProvinceIndex> getProvinceIndex(Integer id, String day) {

        ArrayList<ProvinceIndex> provinceIndices = new ArrayList<>();

        try {
            PreparedStatement stat = con.prepareStatement("select * from ads_province_index where province_id=? and dt=?");

            stat.setInt(1, id);

            stat.setString(2, day);


            ResultSet resultSet = stat.executeQuery();

            /**
             *   province_id INT,
             *    area_code varchar(200),
             *    province_name varchar(200),
             *    region_name varchar(200),
             *    order_amount DECIMAL(10,2),
             *    order_count varchar(200),
             *    dt varchar(200),
             *
             */

            while (resultSet.next()) {
                String area_code = resultSet.getString("area_code");
                String region_name = resultSet.getString("region_name");
                String province_name = resultSet.getString("province_name");
                BigDecimal order_amount = resultSet.getBigDecimal("order_amount");
                long order_count = resultSet.getLong("order_count");

                ProvinceIndex provinceIndex = new ProvinceIndex(id, area_code, region_name, province_name, order_amount, order_count, day);

                provinceIndices.add(provinceIndex);


            }


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return provinceIndices;


    }

}
